package com.wcpdoc.base.dao.impl;

import com.wcpdoc.base.dao.LabelDao;
import com.wcpdoc.base.dao.UserDao;
import com.wcpdoc.base.entity.Label;
import com.wcpdoc.base.entity.User;
import com.wcpdoc.core.dao.impl.RBaseDaoImpl;
import com.wcpdoc.core.entity.PageIn;
import com.wcpdoc.core.entity.PageOut;
import com.wcpdoc.core.util.DateUtil;
import com.wcpdoc.core.util.HibernateUtil;
import com.wcpdoc.core.util.SqlUtil;
import com.wcpdoc.core.util.SqlUtil.Order;
import com.wcpdoc.core.util.ValidateUtil;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 用户数据访问层实现
 * <p>
 * v1.0 zhanghc 2016-6-15下午17:24:19
 */
@Repository
public class LabelDaoImpl extends RBaseDaoImpl<Label> implements LabelDao {
    private static final Logger log = LoggerFactory.getLogger(LabelDaoImpl.class);

    @Override
    public PageOut getListpage(PageIn pageIn) {
        String sql = "SELECT LABEL.ID, LABEL.NAME  FROM  SYS_LABEL LABEL   ";
        SqlUtil sqlUtil = new SqlUtil(sql);
        sqlUtil.addWhere(ValidateUtil.isValid(pageIn.get("name")), "(LABEL.NAME LIKE :NAME )", String.format("%%%s%%", pageIn.get("name")))
                .addOrder("LABEL.UPDATE_TIME", Order.DESC)
                .addWhere("LABEL.STATE != 0");
        System.out.println("=======BulletinDaoImpl 日志打印");
        log.info("=======BulletinDaoImpl 日志打印");
        log.info(sqlUtil.toString());
        PageOut pageOut = getListpage(sqlUtil, pageIn);
        return pageOut;
    }

    @Override
    public List<Map<String, Object>> getListUser(String id) {
        return null;
    }

    @Override
    public Label getLabel(Integer id) {
        String sql = "SELECT LABEL.ID, LABEL.NAME  FROM SYS_LABEL LABEL WHERE LABEL.STATE !=0   ";
        return getEntity(sql, new Object[]{id});
    }

    @Override
    public List<Label> getList() {
        String sql = "SELECT * FROM SYS_LABEL WHERE STATE = 1 ";
        return getList(sql);
    }


/*
	@Override
	public User getUser(String loginName) {
		String sql = "SELECT * FROM SYS_USER WHERE LOGIN_NAME = :LOGIN_NAME AND STATE != 0";
		return getEntity(sql, new Object[] { loginName });
	}

	@Override
	public boolean existLoginName(String loginName, Integer excludeId) {
		if (excludeId == null) {
			String sql = "SELECT COUNT(*) AS NUM FROM SYS_USER WHERE LOGIN_NAME = :LOGIN_NAME AND STATE != 0";
			return getCount(sql, new Object[] { loginName}) > 0;
		}

		String sql = "SELECT COUNT(*) AS NUM FROM SYS_USER WHERE LOGIN_NAME = :LOGIN_NAME AND STATE != 0 AND ID != :ID";
		return getCount(sql, new Object[] { loginName, excludeId }) > 0;
	}

	@Override
	public List<User> getList(Integer orgId) {
		String sql = "SELECT * FROM SYS_USER WHERE ORG_ID = :ORG_ID AND STATE != 0";
		return getList(sql, new Object[] { orgId });
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> getList(Integer[] ids) {
		String sql = "SELECT * FROM SYS_USER WHERE ID IN (:IDS) AND STATE != 0";
		Query<User> query = getCurSession().createSQLQuery(sql);
		query.setParameterList("IDS", ids);
		query.unwrap(NativeQuery.class).addEntity(clazz);
		return query.list();
	}

	@Override
	public List<User> getList() {
		String sql = "SELECT * FROM SYS_USER WHERE STATE != 0";
		return getList(sql);
	}

	@Override
	public List<User> getMarkUserlist(Integer parentId) {
		String sql = "SELECT * FROM SYS_USER WHERE TYPE = 3 AND STATE != 0 AND PARENT_ID = :PARENT_ID";
		return getList(sql, new Object[] { parentId });
	}*/
}
